﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using QuanLyThuoc.Abtract;

namespace QuanLyThuoc
{
    class DanhSachNhomThuoc:AbtDSNhomThuoc
    {
        int n;
        private static List<NhomThuoc> DSNT;
        public DanhSachNhomThuoc()
        { 
            DSNT = new List<NhomThuoc>(); 
        }

        //ghi file
        public void ghiFile()
        {
            Console.WriteLine("Dang ghi...");
            foreach (NhomThuoc nhomThuoc in DSNT)
            {
                nhomThuoc.ghiFileNT();
            }
            Console.WriteLine("Da ghi xong!");
        }
        //doc file
        public void docFile()
        {
            FileStream file = new FileStream("nhomthuoc.txt", FileMode.Open);
            BinaryReader brReader = new BinaryReader(file);
            while (brReader.PeekChar() != -1)
            {
                NhomThuoc nt = new NhomThuoc();
                nt.MaNhomThuoc = brReader.ReadString();
                nt.TenNhomThuoc = brReader.ReadString();
                DSNT.Add(nt);
            }
            brReader.Close();
            file.Close();
        }

        public void NhapDS()
        {
            Console.Write("\nNhap so luong nhom thuoc: ");
           // n = int.Parse(Console.ReadLine());

            //dung vong lap while de bao loi khi nhap sai n
            while (int.TryParse(Console.ReadLine(), out n) == false)
            {
                Console.WriteLine("Nhap sai! Vui long nhap lai!");
            }
            for (int i = 0; i < n; i++)// ko use i?
            {
                NhomThuoc NT = new NhomThuoc();
                NT.NhapNT();
                DSNT.Add(NT);
                NT.ghiFileNT();
            }
        }

        //tim tra ve vi tri nhom thuoc
        /*public int TimNT(string MaNT)
        {
            for (int i = 0; i < n; i++)
            {
                if (DSNT[i].MaNhomThuoc == MaNT)
                    return i;
            }
            return -1;
        }

        //tim tra ve nhom thuoc: ma, ten nhom thuoc
        public NhomThuoc Tim(string Ma)
        {
            for (int i = 0; i < n; i++)
			{
                if (DSNT[i].MaNhomThuoc == Ma)
                    return DSNT[i];
			}
            return null;
        }*/

        //ham tim theo ma
        public static NhomThuoc TimTheoMa(string Ma)
        {
            foreach (NhomThuoc item in DSNT)
            {
                if (Ma == item.MaNhomThuoc)
                {
                    return item;
                }
            }
            return null;
        }

        //tim theo ten
        public List<NhomThuoc> TimTheoTen(string Ten)
        {
            List<NhomThuoc> listNhom = new List<NhomThuoc>(); ;
            foreach (NhomThuoc item in DSNT)
            {
                if (item.TenNhomThuoc.Contains(Ten))
                {
                    listNhom.Add(item);
                }
            }
            return listNhom;
        }

        //tim kiem nhom thuoc
        public override void timKiem()
        {
            int x = -1;
            Console.Write("\nChon chuc nang tim: \n\t1. Theo ma nhom thuoc\n\t2. Theo ten nhom thuoc\n");
            while (int.TryParse(Console.ReadLine(), out x) == false)
            {
                Console.Write("Nhap sai roi! Nhap lai di!");
            }
            switch (x)
            { 
                
                case 1:
                    Console.Write("\nNhap ma nhom thuoc can tim: ");
                    string maNhomTim = Console.ReadLine();
                    NhomThuoc nhomTimMa = TimTheoMa(maNhomTim);
                    Console.Write("\n>>>>>>>>Ket qua tim kiem<<<<<<<<<<<<");
                    if (nhomTimMa != null)
                    {
                        Console.Write("\nMa nhom thuoc \tTen nhom thuoc");
                        nhomTimMa.XuatNT();

                    }
                    else
                        Console.WriteLine("Khong tim thay");
                    break;
                case 2:
                    Console.Write("\nNhap ten nhom thuoc can tim: ");
                    string tenNhomTim = Console.ReadLine();
                    List<NhomThuoc> nhomTimTen = TimTheoTen(tenNhomTim);
                    Console.Write("\n>>>>>>>>Ket qua tim kiem<<<<<<<<<<<<");
                    if (nhomTimTen.Count > 0)
                    {
                        Console.Write("\nMa nhom thuoc \tTen nhom thuoc");
                        foreach (NhomThuoc item in nhomTimTen)
                        {
                            item.XuatNT();
                        }
                    }
                    else
                        Console.WriteLine("Khong tim thay");
                    break;
            }

        }

        //ham xoa 1 nhom thuoc
        public override bool XoaNT(string MaXoa)
        {
            /*int VtXoa = TimNT(MaXoa);
            if (VtXoa != -1) //vi tri ton tai
            {
                DSNT.Remove(DSNT[VtXoa]);
                n--;
                return true;
            }
            return false;*/
            NhomThuoc Vt = TimTheoMa(MaXoa);
            if (Vt != null)
            {
                DSNT.Remove(Vt);
                n--;
                return true;
            }
            return false;
        }

        //ham sua nhom thuoc
        public override bool SuaNT(string MaSua, string TenNhomSua)
        {
            /*int VtSua = TimNT(MaSua);
            if (VtSua != -1)
            {
                DSNT[VtSua].TenNhomThuoc = TenNhomSua;
                return true;
            }
            return false;*/
            NhomThuoc DtSua = TimTheoMa(MaSua);//DtSua: doi tuong sua
            if (DtSua != null)
            {
                DtSua.TenNhomThuoc = TenNhomSua;
                return true;
            }
            return false;
        }

        //ham xuat toan danh sach nhom thuoc
        public override void XuatToanDS()
        {
            //Console.Write("\nDanh sach nhom thuoc");
            Console.Write("\nMa nhom thuoc \tTen nhom thuoc");
            foreach (NhomThuoc item in DSNT)
            {
                item.XuatNT();
            }
        }

        
    }
}
